package com.api.gbhg.lbj.action;

import com.api.gbhg.huangwm.dao.ReplaceJingfeiDao;
import com.api.gbhg.lbj.dao.ReimbursementDao;
import com.api.gbhg.zengping.action.AmountAction;
import com.api.gbhg.zengping.common.CommonDao;
import com.api.gbhg.zengping.dao.PropertyManagementCostDao;
import com.weaver.general.BaseBean;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import weaver.conn.RecordSet;
import weaver.conn.RecordSetTrans;
import weaver.interfaces.workflow.action.Action;
import weaver.soa.workflow.request.DetailTable;
import weaver.soa.workflow.request.RequestInfo;
import weaver.workflow.workflow.WorkflowVersion;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.Map;

import static com.api.gbhg.lbj.tool.Tools.getsfkzsydwbxed;

/**
 * @author lbj
 * @version 1.0
 * @description: TODO 主表报销 归档扣减
 * @date 2021/3/8 10:30
 */
public class ReimbursementAction implements Action {
    private static final Logger logger = LoggerFactory.getLogger("gbhg");
    private   final PropertyManagementCostDao pmc=new PropertyManagementCostDao();
    @Override
    public String execute(RequestInfo requestInfo) {
        logger.info("进入主表报销归档扣减ReimbursementAction");
        String requestid = requestInfo.getRequestid();
        String workflowid = requestInfo.getWorkflowid();
        //标题
        String description = requestInfo.getDescription();
        //当前流程表名-类似 formtable_main_22
        String billTableName = requestInfo.getRequestManager().getBillTableName();
        RecordSet rs = CommonDao.selectByRequestId(new RecordSet(), billTableName, requestid);
        Map<String, String> com1= new HashMap();
        Map<String, String> com2= new HashMap();
        RecordSetTrans rst = new RecordSetTrans();
        rst.setAutoCommit(false);
        String ysxmid =  "";
        if (rs.next()){
            Map<String, String> list = new HashMap<>();
            String bmjjzcfl = rs.getString("bmjjzcfl");
            String ysxm = rs.getString("ysxm");
            ysxmid = ysxm;
            logger.info("预算项目ysxm:"+ysxm);
            String tybxje=rs.getString("tybxje");
            String cxje=rs.getString("bccxjkje");
            if (StringUtils.isBlank(cxje)){
                cxje="0.00";
            }
            String bxje=rs.getString("bxje");
            String mainid = rs.getString("id");
            String zcqd = rs.getString("zcqd");
            String ysly = rs.getString("ysly");
            String fzysrfl = rs.getString("fzysrfl");
            String xzkmdm = rs.getString("xzkmdm");
            String zcdfczbkxm = rs.getString("zcdfczbkxm");
            String nd = rs.getString("nd");
            String dw = rs.getString("szdlhsdw");
            String jbbm = rs.getString("jbbm");
            String bxr = rs.getString("bxr");
            list.put("workflowid", workflowid);//workflowid
            list.put("lcjlid", ysxm);//预算项目
            list.put("tybxje", tybxje);//同意报销金额
            list.put("bmjjzcfl", bmjjzcfl);//部门经济支出分类
            list.put("zcqd", zcqd);//支出渠道
            list.put("ysly", ysly);//预算来源
            list.put("nd", nd);//年度
            list.put("fzysrfl", fzysrfl);//非中央收入分类
            list.put("xzkmdm", xzkmdm);//选择科目代码
            list.put("cxje", cxje);//冲销金额
            list.put("money", bxje);//申请报销金额
            list.put("zcdfczbkxm", zcdfczbkxm);//地方财政拨款项目
            list.put("dw", dw);//单位
            list.put("jbbm", jbbm);//经办部门
            list.put("bxr", bxr);//报销人
            logger.info("冲销金额:"+cxje+"同意报销金额:"+tybxje);
            com1.put("lcjlid", ysxm);//预算项目
            com1.put("bmjjzcfl", bmjjzcfl);//部门经济支出分类

            // 修改状态-bxlcmc-bxlcjlid
            boolean b = pmc.modifyJingfeiSpzt(ysxm,requestid,description);

            //获取报销记录
            RecordSet getJl = new RecordSet();
            Map<String, String> list2 = new HashMap<>();
            String querySql="select * from uf_bxsjjl where lcid="+requestid;
            getJl.execute(querySql);
            if(getJl.next()){
                String ysxm_jl = getJl.getString("ysxm");//
                String sqbxje_jl = getJl.getString("sqbxje");//
                String tybxje_jl = getJl.getString("tybxje");//
                String bmjjfl_jl = getJl.getString("bmjjfl");//
                String cxje_jl = getJl.getString("cxje");//冲销金额
                list2.put("lcjlid", ysxm_jl);//预算项目
                list2.put("bmjjzcfl", bmjjfl_jl);//部门经济支出分类
                list2.put("money", sqbxje_jl);//申请报销金额
                list2.put("tybxje", tybxje_jl.isEmpty()?sqbxje_jl:tybxje_jl);//同意报销金额
                list2.put("cxje", cxje_jl);//冲销金额
                com2.put("lcjlid", getJl.getString("ysxm"));//预算项目
                com2.put("bmjjzcfl", getJl.getString("bmjjfl"));//部门经济支出分类
            }

            // 扣减金额，将冻结金额置为0
            ReimbursementDao rei =new ReimbursementDao();
            boolean b1 = true;
            //2023年12月15日 经费冻结关闭
            //b1 = rei.queryMainJingfei(rst,list,true,0);
            boolean b3 = rei.jingfeilaiyuandonjie2(rst,list,true);//冻结扣减

            //添加报销流程明细记录
            Map<String,Object> data=new HashMap<>();
            //mainid
            data.put("mainid",mainid);
            //报销金额
            data.put("bxje",tybxje);
            //报销流程id
            data.put("bxlcid",requestid);
            //项目流程id
            data.put("xmlcid",ysxm);
            //项目流程名称  根据 ysxm 查==>查经费表
            RecordSet rsj = CommonDao.selectJingfei(ysxm);
            if (rsj.next()){
                //项目名称
                String xmmc = rsj.getString("xmmc");
                data.put("xmlcmc", xmmc);
            }
            //部门经济支出分类
            data.put("bmjjzcfl", rs.getString("bmjjzcfl"));
            //政府经济支出分类
            data.put("zfjjzcfl", rs.getString("zfjjzcfl"));
            //备注
            data.put("bz", "无备注");

            boolean b2=pmc.addbxDetailRecord(data);
            RecordSet rs1 = getsfkzsydwbxed(ysxm);
            rs1.next();
            String sfkzsydwbxed = rs1.getString("sfkzsydwbxed");
            boolean b4=true;
            String lcid = rs1.getString("lcid");
            BaseBean baseBean=new BaseBean();
            String BZ18Workflowid = WorkflowVersion.getActiveVersionWFID(baseBean.getPropValue("FormationWorkflowid", "BZ18Workflowid"));//读取配置文件
            logger.info("是否控制使用单位报销额度:"+sfkzsydwbxed+",workflowid:"+lcid+",BZ18Workflowid:"+BZ18Workflowid);

            if (StringUtils.isNotBlank(sfkzsydwbxed)&&sfkzsydwbxed.equals("1")&&!lcid.equals(BZ18Workflowid)){
                b4 = rei.userQuotaFreezeDeduction(rst,list, true);
            }
            if(!b1||!b||!b2||!b3||!b4){
                rst.rollback();
                //删除报销记录
                RecordSet newRS = new RecordSet();
                newRS.execute("DELETE FROM uf_bxjl_kj WHERE lcid="+requestid);
                newRS.execute("DELETE FROM uf_bxjl_kj_dt1 WHERE lcid="+requestid);
                newRS.execute("DELETE FROM uf_bxjl_kj_dt2 WHERE lcid="+requestid);

                ReplaceJingfeiDao replaceJingfei = new ReplaceJingfeiDao();
                Map<String,Object> result = replaceJingfei.replace(ysxmid);
                boolean jingfei = (boolean) result.get("result1");
                boolean jingfeilaiyuan = (boolean) result.get("result2");

                logger.info("============================归档后扣减金额出现错误，修改审批状态错误======================================");
                requestInfo.getRequestManager().setMessagecontent("归档后扣减金额出现错误，修改审批状态错误");
                requestInfo.getRequestManager().setMessageid("1");
                return FAILURE_AND_CONTINUE;
             }
        }

        try {
            ReplaceJingfeiDao replaceJingfei = new ReplaceJingfeiDao();

            Map<String,Object> result = replaceJingfei.replace(ysxmid);
            boolean jingfei = (boolean) result.get("result1");
            boolean jingfeilaiyuan = (boolean) result.get("result1");
            if(!jingfei){
                //删除报销记录
                rs.execute("DELETE FROM uf_bxjl_kj WHERE lcid="+requestid);
                rs.execute("DELETE FROM uf_bxjl_kj_dt1 WHERE lcid="+requestid);
                rs.execute("DELETE FROM uf_bxjl_kj_dt2 WHERE lcid="+requestid);
                rst.rollback();
                requestInfo.getRequestManager().setMessagecontent("归档后重新计算额度失败 jingfei错误");
                requestInfo.getRequestManager().setMessageid("1");
                return FAILURE_AND_CONTINUE;
            }
            if(!jingfeilaiyuan){
                //删除报销记录
                rs.execute("DELETE FROM uf_bxjl_kj WHERE lcid="+requestid);
                rs.execute("DELETE FROM uf_bxjl_kj_dt1 WHERE lcid="+requestid);
                rs.execute("DELETE FROM uf_bxjl_kj_dt2 WHERE lcid="+requestid);
                rst.rollback();
                requestInfo.getRequestManager().setMessagecontent("归档后重新计算额度失败 jingfeilaiyuan错误");
                requestInfo.getRequestManager().setMessageid("1");
                return FAILURE_AND_CONTINUE;
            }

        }catch ( Exception e){
            //删除报销记录
            rs.execute("DELETE FROM uf_bxjl_kj WHERE lcid="+requestid);
            rs.execute("DELETE FROM uf_bxjl_kj_dt1 WHERE lcid="+requestid);
            rs.execute("DELETE FROM uf_bxjl_kj_dt2 WHERE lcid="+requestid);
            e.printStackTrace();
            rst.rollback();
            requestInfo.getRequestManager().setMessagecontent("归档后重新计算额度异常");
            requestInfo.getRequestManager().setMessageid("1");
            return FAILURE_AND_CONTINUE;
        }
        rst.commit();
        return SUCCESS;
    }
}

